/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tracker.entity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Workshop Class ID : SE0691 Student ID : SE90009 Student Name : Phạm Nguyễn
 * Nhật Trung Due Date : 12 November 2012 I declare that this assignment is my
 * own work in accordance with FPT Policy.
 */
@Stateless
public class PointsEntityFacade extends AbstractFacade<PointsEntity> {

    @PersistenceContext(unitName = "ASG2-Tracker1-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PointsEntityFacade() {
        super(PointsEntity.class);
    }
    /*
     * List All Torrents which are belong to the user whose id equals userId
     */

    public List<PointsEntity> findAllTorrentsByUserId(int userId) {
        List<PointsEntity> listPoints = findAll();
        List<PointsEntity> listTorrents = new ArrayList<PointsEntity>();
        System.out.println("Find Torrent Begin!");
        if (!listPoints.isEmpty()) {
            Iterator<PointsEntity> it = listPoints.iterator();
            while (it.hasNext()) {
                PointsEntity entity = it.next();
                if (entity.getUserId() != null) {
                    if (entity.getUserId().getUserId() == userId) {
                        listTorrents.add(entity);
                    }
                }
            }
            System.out.println("Find Torrents Successfully!");
        } else {
            System.out.println("Find Torrents Failed!");
        }

        return listTorrents;
    }
    /*List all Torrents which are belonged to a user and be uploaded(torrentType=1) or 
     * leeching (torrentType = 2) or seeding (torrentType = 3)
     */

    public List<PointsEntity> findAllTorrentsOfUserIdByTorrentType(int userId, int TorrentType) {
        List<PointsEntity> list = findAllTorrentsByUserId(userId);
        List<PointsEntity> listTorrents = new ArrayList<PointsEntity>();
        if (!list.isEmpty()) {
            Iterator<PointsEntity> it = list.iterator();
            while (it.hasNext()) {
                PointsEntity entity = it.next();
                if (entity.getTorrentType().equals(TorrentType)) {
                    listTorrents.add(entity);
                }
            }

        }
        return listTorrents;
    }

    /*
     * Get all point of a user (userId) based on the individual point of each torrent of 3 type
     * then sum up
     */
    public float getPointsDependTorrentsByUserId(int userId) {
        List<PointsEntity> list = findAllTorrentsByUserId(userId);
        Float TotalPoint = 0.0F;
        if (!list.isEmpty()) {
            Iterator<PointsEntity> it = list.iterator();
            while (it.hasNext()) {
                PointsEntity entity = it.next();
                if (entity.getTorrentType().equals(1)) { //1: Upload Torrent Type
                    TotalPoint += entity.getPoint() * 3 + 1;
                }
                if (entity.getTorrentType().equals(2)) {//2: Leech Torrent Type
                    TotalPoint += entity.getPoint() * 2 + 1;
                }
                if (entity.getTorrentType().equals(3)) {//3: Seed Torrent Type
                    TotalPoint += entity.getPoint() * 1 + 1;
                }
            }
        }
        return TotalPoint;
    }

    /*
     * Find all users who interact with this torrent (torrentId)
     */
    public List<PointsEntity> findAllUsersByTorrentId(int torrentId) {
        List<PointsEntity> list = findAll();
        List<PointsEntity> torrentList = new ArrayList<PointsEntity>();;
        System.out.println("Find User Begin!");
        if (!list.isEmpty()) {
            Iterator<PointsEntity> it = list.iterator();
            while (it.hasNext()) {
                PointsEntity entity = it.next();
                if (entity.getTorrentId() != null) {
                    System.out.println("Torrent Id: " + entity.getTorrentId().getTorrentId());
                    System.out.println("Compare: " + entity.getTorrentId().getTorrentId().equals(torrentId));
                    if (entity.getTorrentId().getTorrentId().equals(torrentId)) {
                        torrentList.add(entity);
                    }
                }
            }
            System.out.println("Find User Successfully");
        } else {
            System.out.println("Find User Failed");
        }
        return torrentList;
    }
    
    /*
     * List all users who is uploading (torrentType = 1) or leeching (torrentType = 2)
     * or seeding (torrentType = 3) of a specific torrent (torrentId)
     */
    public List<PointsEntity> findAllUserByTorrentIdOfTypeId(int torrentId, int torrentType) {
        List<PointsEntity> list = findAllUsersByTorrentId(torrentId);
        List<PointsEntity> listTorrents = new ArrayList<PointsEntity>();;
        if (!list.isEmpty()) {
            Iterator<PointsEntity> it = list.iterator();
            while (it.hasNext()) {
                PointsEntity entity = it.next();
                if (entity.getTorrentType().equals(torrentType)) {
                    listTorrents.add(entity);
                }
            }
        }
            return listTorrents;
        }


    }
